﻿//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-5-11 13:14:22
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL.TaoBao;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL.TaoBao
{
    /// <summary>
    /// 数据访问类 TB_BAOBEI
    /// </summary>
    public class TB_BAOBEI : ITB_BAOBEI
    {
        public TB_BAOBEI()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.TaoBao.TB_BAOBEI model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@LM_ID", SqlDbType.Int),
                    new SqlParameter("@DM_ID", SqlDbType.Int),
                    new SqlParameter("@XJCD", SqlDbType.VarChar,50),
                    new SqlParameter("@DQ_ID", SqlDbType.Int),
                    new SqlParameter("@CSFS", SqlDbType.VarChar,50),
                    new SqlParameter("@YKJ", SqlDbType.Decimal),
                    new SqlParameter("@TGJ", SqlDbType.Decimal),
                    new SqlParameter("@TGSL", SqlDbType.Int),
                    new SqlParameter("@BBSL", SqlDbType.Int),
                    new SqlParameter("@JJ", SqlDbType.Int),
                    new SqlParameter("@JJE", SqlDbType.Decimal),
                    new SqlParameter("@YXQ", SqlDbType.VarChar,50),
                    new SqlParameter("@YFCD", SqlDbType.VarChar,50),
                    new SqlParameter("@FKFS", SqlDbType.VarChar,50),
                    new SqlParameter("@YFMB", SqlDbType.Int),
                    new SqlParameter("@PY", SqlDbType.Decimal),
                    new SqlParameter("@KD", SqlDbType.Decimal),
                    new SqlParameter("@EMS", SqlDbType.Decimal),
                    new SqlParameter("@CFB", SqlDbType.Int),
                    new SqlParameter("@FP", SqlDbType.Int),
                    new SqlParameter("@BX", SqlDbType.Int),
                    new SqlParameter("@CF", SqlDbType.Int),
                    new SqlParameter("@FC", SqlDbType.Int),
                    new SqlParameter("@CC", SqlDbType.Int),
                    new SqlParameter("@HYDZ", SqlDbType.Int),
                    new SqlParameter("@DSFB", SqlDbType.Int),
                    new SqlParameter("@FBSJ", SqlDbType.DateTime),
                    new SqlParameter("@XQGS", SqlDbType.VarChar,20),
                    new SqlParameter("@BBMS", SqlDbType.VarChar,20),
                    new SqlParameter("@ZD", SqlDbType.Int),
                    new SqlParameter("@TY", SqlDbType.Int),
                    new SqlParameter("@TJ", SqlDbType.Int),
                    new SqlParameter("@TJRQ", SqlDbType.DateTime),
                    new SqlParameter("@IsChanged", SqlDbType.Int),
                    new SqlParameter("@BBMC", SqlDbType.VarChar,100),
                    new SqlParameter("@MB_ID", SqlDbType.Int),
                    new SqlParameter("@SP_ID", SqlDbType.Int),
                    new SqlParameter("@GG1_ID", SqlDbType.Int),
                    new SqlParameter("@DQDM", SqlDbType.VarChar,20),
                    new SqlParameter("@DQMC", SqlDbType.VarChar,100),
                    new SqlParameter("@LMDM", SqlDbType.VarChar,50),
                    new SqlParameter("@LMMC", SqlDbType.VarChar,200),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.TN_ID;
            parameters[2].Value = model.PID;
            parameters[3].Value = model.LM_ID;
            parameters[4].Value = model.DM_ID;

            if (model.XJCD != null)
                parameters[5].Value = model.XJCD;
            else
                parameters[5].Value = DBNull.Value;

            parameters[6].Value = model.DQ_ID;

            if (model.CSFS != null)
                parameters[7].Value = model.CSFS;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.YKJ;
            parameters[9].Value = model.TGJ;
            parameters[10].Value = model.TGSL;
            parameters[11].Value = model.BBSL;
            parameters[12].Value = model.JJ;
            parameters[13].Value = model.JJE;

            if (model.YXQ != null)
                parameters[14].Value = model.YXQ;
            else
                parameters[14].Value = DBNull.Value;


            if (model.YFCD != null)
                parameters[15].Value = model.YFCD;
            else
                parameters[15].Value = DBNull.Value;


            if (model.FKFS != null)
                parameters[16].Value = model.FKFS;
            else
                parameters[16].Value = DBNull.Value;

            parameters[17].Value = model.YFMB;
            parameters[18].Value = model.PY;
            parameters[19].Value = model.KD;
            parameters[20].Value = model.EMS;
            parameters[21].Value = model.CFB;
            parameters[22].Value = model.FP;
            parameters[23].Value = model.BX;
            parameters[24].Value = model.CF;
            parameters[25].Value = model.FC;
            parameters[26].Value = model.CC;
            parameters[27].Value = model.HYDZ;
            parameters[28].Value = model.DSFB;

            if (model.FBSJ != DateTime.MinValue)
                parameters[29].Value = model.FBSJ;
            else
                parameters[29].Value = DBNull.Value;


            if (model.XQGS != null)
                parameters[30].Value = model.XQGS;
            else
                parameters[30].Value = DBNull.Value;


            if (model.BBMS != null)
                parameters[31].Value = model.BBMS;
            else
                parameters[31].Value = DBNull.Value;

            parameters[32].Value = model.ZD;
            parameters[33].Value = model.TY;
            parameters[34].Value = model.TJ;

            if (model.TJRQ != DateTime.MinValue)
                parameters[35].Value = model.TJRQ;
            else
                parameters[35].Value = DBNull.Value;

            parameters[36].Value = model.IsChanged;

            if (model.BBMC != null)
                parameters[37].Value = model.BBMC;
            else
                parameters[37].Value = DBNull.Value;

            parameters[38].Value = model.MB_ID;
            parameters[39].Value = model.SP_ID;
            parameters[40].Value = model.GG1_ID;

            if (model.DQDM != null)
                parameters[41].Value = model.DQDM;
            else
                parameters[41].Value = DBNull.Value;


            if (model.DQMC != null)
                parameters[42].Value = model.DQMC;
            else
                parameters[42].Value = DBNull.Value;


            if (model.LMDM != null)
                parameters[43].Value = model.LMDM;
            else
                parameters[43].Value = DBNull.Value;


            if (model.LMMC != null)
                parameters[44].Value = model.LMMC;
            else
                parameters[44].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[45].Value = model.field1;
            else
                parameters[45].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[46].Value = model.field2;
            else
                parameters[46].Value = DBNull.Value;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_BAOBEI_ADD", parameters, out rowsAffected);

            if (rowsAffected > 0)
                return (int)parameters[0].Value;
            else
                return 0;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.TaoBao.TB_BAOBEI model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int),
                    new SqlParameter("@TN_ID", SqlDbType.Int),
                    new SqlParameter("@PID", SqlDbType.Int),
                    new SqlParameter("@LM_ID", SqlDbType.Int),
                    new SqlParameter("@DM_ID", SqlDbType.Int),
                    new SqlParameter("@XJCD", SqlDbType.VarChar,50),
                    new SqlParameter("@DQ_ID", SqlDbType.Int),
                    new SqlParameter("@CSFS", SqlDbType.VarChar,50),
                    new SqlParameter("@YKJ", SqlDbType.Decimal),
                    new SqlParameter("@TGJ", SqlDbType.Decimal),
                    new SqlParameter("@TGSL", SqlDbType.Int),
                    new SqlParameter("@BBSL", SqlDbType.Int),
                    new SqlParameter("@JJ", SqlDbType.Int),
                    new SqlParameter("@JJE", SqlDbType.Decimal),
                    new SqlParameter("@YXQ", SqlDbType.VarChar,50),
                    new SqlParameter("@YFCD", SqlDbType.VarChar,50),
                    new SqlParameter("@FKFS", SqlDbType.VarChar,50),
                    new SqlParameter("@YFMB", SqlDbType.Int),
                    new SqlParameter("@PY", SqlDbType.Decimal),
                    new SqlParameter("@KD", SqlDbType.Decimal),
                    new SqlParameter("@EMS", SqlDbType.Decimal),
                    new SqlParameter("@CFB", SqlDbType.Int),
                    new SqlParameter("@FP", SqlDbType.Int),
                    new SqlParameter("@BX", SqlDbType.Int),
                    new SqlParameter("@CF", SqlDbType.Int),
                    new SqlParameter("@FC", SqlDbType.Int),
                    new SqlParameter("@CC", SqlDbType.Int),
                    new SqlParameter("@HYDZ", SqlDbType.Int),
                    new SqlParameter("@DSFB", SqlDbType.Int),
                    new SqlParameter("@FBSJ", SqlDbType.DateTime),
                    new SqlParameter("@XQGS", SqlDbType.VarChar,20),
                    new SqlParameter("@BBMS", SqlDbType.VarChar,20),
                    new SqlParameter("@ZD", SqlDbType.Int),
                    new SqlParameter("@TY", SqlDbType.Int),
                    new SqlParameter("@TJ", SqlDbType.Int),
                    new SqlParameter("@TJRQ", SqlDbType.DateTime),
                    new SqlParameter("@IsChanged", SqlDbType.Int),
                    new SqlParameter("@BBMC", SqlDbType.VarChar,100),
                    new SqlParameter("@MB_ID", SqlDbType.Int),
                    new SqlParameter("@SP_ID", SqlDbType.Int),
                    new SqlParameter("@GG1_ID", SqlDbType.Int),
                    new SqlParameter("@DQDM", SqlDbType.VarChar,20),
                    new SqlParameter("@DQMC", SqlDbType.VarChar,100),
                    new SqlParameter("@LMDM", SqlDbType.VarChar,50),
                    new SqlParameter("@LMMC", SqlDbType.VarChar,200),
                    new SqlParameter("@field1", SqlDbType.VarChar,255),
                    new SqlParameter("@field2", SqlDbType.VarChar,255)
            };
            parameters[0].Value = model.MID;
            parameters[1].Value = model.TN_ID;
            parameters[2].Value = model.PID;
            parameters[3].Value = model.LM_ID;
            parameters[4].Value = model.DM_ID;

            if (model.XJCD != null)
                parameters[5].Value = model.XJCD;
            else
                parameters[5].Value = DBNull.Value;

            parameters[6].Value = model.DQ_ID;

            if (model.CSFS != null)
                parameters[7].Value = model.CSFS;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.YKJ;
            parameters[9].Value = model.TGJ;
            parameters[10].Value = model.TGSL;
            parameters[11].Value = model.BBSL;
            parameters[12].Value = model.JJ;
            parameters[13].Value = model.JJE;

            if (model.YXQ != null)
                parameters[14].Value = model.YXQ;
            else
                parameters[14].Value = DBNull.Value;


            if (model.YFCD != null)
                parameters[15].Value = model.YFCD;
            else
                parameters[15].Value = DBNull.Value;


            if (model.FKFS != null)
                parameters[16].Value = model.FKFS;
            else
                parameters[16].Value = DBNull.Value;

            parameters[17].Value = model.YFMB;
            parameters[18].Value = model.PY;
            parameters[19].Value = model.KD;
            parameters[20].Value = model.EMS;
            parameters[21].Value = model.CFB;
            parameters[22].Value = model.FP;
            parameters[23].Value = model.BX;
            parameters[24].Value = model.CF;
            parameters[25].Value = model.FC;
            parameters[26].Value = model.CC;
            parameters[27].Value = model.HYDZ;
            parameters[28].Value = model.DSFB;

            if (model.FBSJ != DateTime.MinValue)
                parameters[29].Value = model.FBSJ;
            else
                parameters[29].Value = DBNull.Value;


            if (model.XQGS != null)
                parameters[30].Value = model.XQGS;
            else
                parameters[30].Value = DBNull.Value;


            if (model.BBMS != null)
                parameters[31].Value = model.BBMS;
            else
                parameters[31].Value = DBNull.Value;

            parameters[32].Value = model.ZD;
            parameters[33].Value = model.TY;
            parameters[34].Value = model.TJ;

            if (model.TJRQ != DateTime.MinValue)
                parameters[35].Value = model.TJRQ;
            else
                parameters[35].Value = DBNull.Value;

            parameters[36].Value = model.IsChanged;

            if (model.BBMC != null)
                parameters[37].Value = model.BBMC;
            else
                parameters[37].Value = DBNull.Value;

            parameters[38].Value = model.MB_ID;
            parameters[39].Value = model.SP_ID;
            parameters[40].Value = model.GG1_ID;

            if (model.DQDM != null)
                parameters[41].Value = model.DQDM;
            else
                parameters[41].Value = DBNull.Value;


            if (model.DQMC != null)
                parameters[42].Value = model.DQMC;
            else
                parameters[42].Value = DBNull.Value;


            if (model.LMDM != null)
                parameters[43].Value = model.LMDM;
            else
                parameters[43].Value = DBNull.Value;


            if (model.LMMC != null)
                parameters[44].Value = model.LMMC;
            else
                parameters[44].Value = DBNull.Value;


            if (model.field1 != null)
                parameters[45].Value = model.field1;
            else
                parameters[45].Value = DBNull.Value;


            if (model.field2 != null)
                parameters[46].Value = model.field2;
            else
                parameters[46].Value = DBNull.Value;


            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_BAOBEI_Update", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;

            SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_BAOBEI_Delete", parameters, out rowsAffected);
            return rowsAffected > 0;
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            int rowsAffected;
            SqlParameter[] parameters ={ };
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_BAOBEI_GetMaxId", parameters, out rowsAffected);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int MID)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            return SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_BAOBEI_Exists", parameters, out rowsAffected) == 1;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.TaoBao.TB_BAOBEI GetModel(int MID)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@MID", SqlDbType.Int)};
            parameters[0].Value = MID;
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_BAOBEI_GetModel", parameters, "ds");
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_BAOBEI> GetList()
        {
            SqlParameter[] parameters ={ };
            DataSet ds = SqlHelper.RunProcedure(SqlHelper.LocalSqlServer, "sp_TB_BAOBEI_GetList", parameters, "ds");
            return GetList(ds);
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.TaoBao.TB_BAOBEI> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[TB_BAOBEI]", null, pageSize, pageIndex, fldSort, sort, strCondition, "MID", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.TaoBao.TB_BAOBEI GetModel(DataRow r)
        {
            Model.TaoBao.TB_BAOBEI model = new Model.TaoBao.TB_BAOBEI();
            model.MID = SqlHelper.GetInt(r["MID"]);
            model.TN_ID = SqlHelper.GetInt(r["TN_ID"]);
            model.PID = SqlHelper.GetInt(r["PID"]);
            model.LM_ID = SqlHelper.GetInt(r["LM_ID"]);
            model.DM_ID = SqlHelper.GetInt(r["DM_ID"]);
            model.XJCD = SqlHelper.GetString(r["XJCD"]);
            model.DQ_ID = SqlHelper.GetInt(r["DQ_ID"]);
            model.CSFS = SqlHelper.GetString(r["CSFS"]);
            model.YKJ = SqlHelper.GetDecimal(r["YKJ"]);
            model.TGJ = SqlHelper.GetDecimal(r["TGJ"]);
            model.TGSL = SqlHelper.GetInt(r["TGSL"]);
            model.BBSL = SqlHelper.GetInt(r["BBSL"]);
            model.JJ = SqlHelper.GetInt(r["JJ"]);
            model.JJE = SqlHelper.GetDecimal(r["JJE"]);
            model.YXQ = SqlHelper.GetString(r["YXQ"]);
            model.YFCD = SqlHelper.GetString(r["YFCD"]);
            model.FKFS = SqlHelper.GetString(r["FKFS"]);
            model.YFMB = SqlHelper.GetInt(r["YFMB"]);
            model.PY = SqlHelper.GetDecimal(r["PY"]);
            model.KD = SqlHelper.GetDecimal(r["KD"]);
            model.EMS = SqlHelper.GetDecimal(r["EMS"]);
            model.CFB = SqlHelper.GetInt(r["CFB"]);
            model.FP = SqlHelper.GetInt(r["FP"]);
            model.BX = SqlHelper.GetInt(r["BX"]);
            model.CF = SqlHelper.GetInt(r["CF"]);
            model.FC = SqlHelper.GetInt(r["FC"]);
            model.CC = SqlHelper.GetInt(r["CC"]);
            model.HYDZ = SqlHelper.GetInt(r["HYDZ"]);
            model.DSFB = SqlHelper.GetInt(r["DSFB"]);
            model.FBSJ = SqlHelper.GetDateTime(r["FBSJ"]);
            model.XQGS = SqlHelper.GetString(r["XQGS"]);
            model.BBMS = SqlHelper.GetString(r["BBMS"]);
            model.ZD = SqlHelper.GetInt(r["ZD"]);
            model.TY = SqlHelper.GetInt(r["TY"]);
            model.TJ = SqlHelper.GetInt(r["TJ"]);
            model.TJRQ = SqlHelper.GetDateTime(r["TJRQ"]);
            model.IsChanged = SqlHelper.GetInt(r["IsChanged"]);
            model.BBMC = SqlHelper.GetString(r["BBMC"]);
            model.MB_ID = SqlHelper.GetInt(r["MB_ID"]);
            model.SP_ID = SqlHelper.GetInt(r["SP_ID"]);
            model.GG1_ID = SqlHelper.GetInt(r["GG1_ID"]);
            model.DQDM = SqlHelper.GetString(r["DQDM"]);
            model.DQMC = SqlHelper.GetString(r["DQMC"]);
            model.LMDM = SqlHelper.GetString(r["LMDM"]);
            model.LMMC = SqlHelper.GetString(r["LMMC"]);
            model.field1 = SqlHelper.GetString(r["field1"]);
            model.field2 = SqlHelper.GetString(r["field2"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.TaoBao.TB_BAOBEI> GetList(DataSet ds)
        {
            List<Model.TaoBao.TB_BAOBEI> l = new List<Model.TaoBao.TB_BAOBEI>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
